var appmark = "G1v8KCW00mQ46442";
var appword = "CPm861X63j9302r3";
var decodekey = "564DA474187BFD5BDED2A9C8657DDD650ECDBC56216585F51C5A03F3EF115693";

//需要执行的信息,第一步走onGetUserTicket()
onGetUserTicket();
//根据票据ticket信息获取token
function ticketValidate(ticket) {
    var nowDate = getNowFormatDate();
    var sign = appmark + appword + nowDate;
    sign = hex_md5(sign);
    var paramData = {
        "ticket": ticket
    };
    var reqData = {
        appmark: appmark,
        time: nowDate,
        sign: sign,
        servicename: "ticketValidate",
        params: JSON.stringify(paramData)
    };

    lightAppJssdk.request.request({
        url: 'http://zwfw.sd.gov.cn/sdsfjis/gateway/interface.do', //请求地址
        data: reqData, //请求参数
        header: '', //请求头设置
        dataType: 'json', //返回数据格式，默认json
        async: false, //同步设置
        success: function(data) { //成功回调
            //alert('ticketValidate返回数据====='+JSON.stringify(data));
            if(data.retcode == "000000") {
                var jsonData = JSON.parse(data.data);
                token = jsonData.token;
                findUserByToken(token);
            }
        },
        fail: function(data) {
            //alert(data);
        }
    });
}
//根据token信息获取UUID
function findUserByToken(token) {
    var nowDate = getNowFormatDate();
    var sign = appmark + appword + nowDate;
    //MD5
    sign = hex_md5(sign);
    var paramData = {
        "token": token
    };
    var reqData = {
        appmark: appmark,
        time: nowDate,
        sign: sign,
        servicename: "findUserByToken",
        params: JSON.stringify(paramData)
    };

    lightAppJssdk.request.request({
        url: 'http://zwfw.sd.gov.cn/sdsfjis/gateway/interface.do', //请求地址
        data: reqData, //请求参数
        header: '', //请求头设置
        dataType: 'json', //返回数据格式，默认json
        async: false, //同步设置
        success: function(data) { //成功回调
            //alert('findUserByToken返回数据====='+JSON.stringify(data));
            if(data.retcode == "000000") {
                uuid = data.data;
                SM2decode(uuid, token);
            }

        },
        fail: function(data) {
            //alert(data);
        }
    });
}

//SM2
function SM2decode(uuid, token) {
    var decUuid = "";
    var nowDate = getNowFormatDate();
    var sign = appmark + appword + nowDate;
    sign = hex_md5(sign);
    var paramData = {
        "decodetext": uuid,
        "decodekey": decodekey
    };
    var reqData = {
        appmark: appmark,
        time: nowDate,
        sign: sign,
        servicename: "SM2decode",
        params: JSON.stringify(paramData)
    };

    lightAppJssdk.request.request({
        url: 'http://zwfw.sd.gov.cn/sdsfjis/gateway/interface.do', //请求地址
        data: reqData, //请求参数
        header: '', //请求头设置
        dataType: 'json', //返回数据格式，默认json
        async: false, //同步设置
        success: function(data) { //成功回调
            //alert('SM2decode返回数据====='+JSON.stringify(data));
            if(data.retcode == "000000") {
                var jsonData = JSON.parse(data.data);
                decUuid = jsonData.uuid;
                //alert("decUuid======"+decUuid);
                findUserByTokenAndUuid(token, decUuid);
            }

        },
        fail: function(data) {
            //alert(data);
        }
    });
}

function findUserByTokenAndUuid(token, decUuid) {
    var nowDate = getNowFormatDate();
    var sign = appmark + appword + nowDate;
    sign = hex_md5(sign);
    var paramData = {
        "token": token,
        "uuid": decUuid
    };
    var reqData = {
        appmark: appmark,
        time: nowDate,
        sign: sign,
        servicename: "findUserByTokenAndUuid",
        params: JSON.stringify(paramData)
    };

    lightAppJssdk.request.request({
        url: 'http://zwfw.sd.gov.cn/sdsfjis/gateway/interface.do', //请求地址
        data: reqData, //请求参数
        header: '', //请求头设置
        dataType: 'json', //返回数据格式，默认json
        async: false, //同步设置
        success: function(data) { //成功回调
            //alert('findUserByTokenAndUuid返回数据====='+JSON.stringify(data));
            if(data.retcode == "000000") {
                var userData = data.data;
                SM2Userdecode(userData);
            }
        },
        fail: function(data) {
            alert(data);
        }
    });
}

function SM2Userdecode(userData) {
    var decUserData = "";
    var nowDate = getNowFormatDate();
    var sign = appmark + appword + nowDate;
    sign = hex_md5(sign);
    var paramData = {
        "decodetext": userData,
        "decodekey": decodekey
    };
    var reqData = {
        appmark: appmark,
        time: nowDate,
        sign: sign,
        servicename: "SM2decode",
        params: JSON.stringify(paramData)
    };
    lightAppJssdk.request.request({
        url: 'http://zwfw.sd.gov.cn/sdsfjis/gateway/interface.do', //请求地址
        data: reqData, //请求参数
        header: '', //请求头设置
        dataType: 'json', //返回数据格式，默认json
        async: false, //同步设置
        success: function(data) { //成功回调
            //alert('SM2decode返回的用户数据====='+JSON.stringify(data));
            if(data.retcode == "000000") {
                var obj = JSON.parse(data.data);
                //								alert(JSON.stringify(obj));
                //先判断是否实名：1-已实名；0-未实名
                if(obj.isauthuser == '1') {
                    infoText(obj.cardid);
                } else {
                    window.location.href = "http://isdapp.shandong.gov.cn/jmopen/webapp/html5/youhaotishi/index.html";
                }
            }
        },
        fail: function(data) {
            alert(data);
        }
    });
}

function getNowFormatDate() {
    var date = new Date();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if(month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if(strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getYear() + month + strDate + date.getHours() + date.getMinutes() + date.getSeconds();
    return currentdate;
}

//获得用户票据
function onGetUserTicket() {
    //获取登陆用户得ticket
    lightAppJssdk.user.getTicket({
        success: function(data) { //成功回调
            if(data == '未登录') {
                onLoginApp();
            } else {
                lightAppJssdk.notification.showPreloader ({
                    text: "加载中..", //loading显示的字符，空表示不显示文字
                    success:function(data){ //成功回调
                    },
                    fail:function(data){ //错误返回
                    }
                });
                var jsonData = JSON.parse(data);
                if(jsonData.retcode == "000000") {
                    jsonData = JSON.parse(jsonData.data);
                    ticket = jsonData.ticket;
                    ticketValidate(ticket);
                }
            }
        },
        fail: function(data) { //错误返回
            alert("fail"+data);
        }
    });
};

//登录
function onLoginApp() {
    lightAppJssdk.user.loginapp({
        success: function(data) {
            if(data == '未登录') {
                oncloseWindow();
            } else {
                var jsonData = JSON.parse(data);
                if(jsonData.retcode == "000000") {
                    jsonData = JSON.parse(jsonData.data);
                    ticket = jsonData.ticket;
                    ticketValidate(ticket);
                }
            }
        },
        fail: function(data) {
            document.getElementById('loading').style.display = 'none';
            mui.alert("加载失败");
            oncloseWindow();
        }
    });
};

//网络状态
function onNetworkType() {
    // config('528e07d64902423fbe6e4d59019f7508','9d68a2e965e8ae2c5d284fe5e9f4e674');
    networkType({
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 用户注册（浙江为例）
function onRegisterUser() {
    registerUser({
        success: function(data) {
            //alert(data);
        },
        fail: function(data) {
            //alert(data);
        }
    });

};

// 找回密码（浙江为例）
function onModifyPassword() {
    modifyPassword({
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 获得用户信息
function onGetUserInfo() {
    getUserInfo({
        success: function(data) {
            // 首先要判断登陆类型 登陆的条件下 data.type 1 个人 2 法人
            document.getElementById('loading').style.display = 'none';
            if(data == '未登录') {
                onLoginApp();
            } else {
                var obj = JSON.parse(data);
                if(obj.type == '1') {
                    // 个人 先判断是否实名
                    if(obj.grade != 2) {
                        window.location.href = "http://app.jinan.gov.cn/jmportal/djts/djtsx.html";
                    } else {
                        window.localStorage.realname = obj.realname;
                        window.localStorage.cardid = obj.cardid;
                        document.getElementById("name").innerHTML = localStorage.realname;
                        document.getElementById("ss_num").innerHTML = localStorage.cardid.substr(0, 4) + "****" + localStorage.cardid.substr(14, 4);
                    }
                } else if(obj.type == '2') {
                    // 法人
                    window.location.href = "http://jnzwfw.sd.gov.cn/jmportal/djts/yhpd.html";
                }
            }
        },
        fail: function(data) {
            document.getElementById('loading').style.display = 'none';
            mui.alert("加载失败");
        }
    });
};

// 注销
function onLogout() {
    logout({
        success: function(data) {
            //alert(data);
        },
        fail: function(data) {
            //alert(data);
        }
    });
};

// 登录到qq
function onLoginQQ() {
    loginQQ({
        success: function(data) {
            if(typeof data === 'string' && data.constructor == String) {
                alert(data);
            } else {
                var userName = data.userName; // 用户昵称
                var userIcon = data.userIcon; // 用户头像的url
                var userId = data.userId; // 用户id
                alert("用户昵称:" + userName + "\n用户头像url:" + userIcon + "\n用户ID:" +
                    userId);
            }
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 注销qq
function onLogoutQQ() {
    logoutQQ({
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 登录到腾讯微博
function onLoginTencentWeibo() {
    loginTencentWeibo({
        success: function(data) {
            if(typeof data === 'string' && data.constructor == String) {
                alert(data);
            } else {
                var userName = data.userName; // 用户昵称
                var userIcon = data.userIcon; // 用户头像的url
                var userId = data.userId; // 用户id
                alert("用户昵称:" + userName + "\n用户头像url:" + userIcon + "\n用户ID:" +
                    userId);
            }

        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 注销腾讯微博
function onLogoutTencentWeibo() {
    logoutTencentWeibo({
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 登录到新浪微博
function onLoginSinaWeibo() {
    loginSinaWeibo({
        success: function(data) {
            if(typeof data === 'string' && data.constructor == String) {
                alert(data);
            } else {
                var userName = data.userName; // 用户昵称
                var userIcon = data.userIcon; // 用户头像的url
                var userId = data.userId; // 用户id
                alert("用户昵称:" + userName + "\n用户头像url:" + userIcon + "\n用户ID:" +
                    userId);
            }

        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 注销新浪微博
function onLogoutSinaWeibo() {
    logoutSinaWeibo({
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};

// 获得设备唯一标示
function onUUID() {
    getUUID({
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};

// 计算距离
function onGetDistance(arg0) {
    getDistance({
        arg: arg0,
        success: function(data) {
            alert("距离:" + data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};

// 获得坐标
function onLocation() {
    getlocation({
        success: function(data) {
            if(typeof data === 'string' && data.constructor == String) {
                alert(data);
            } else {
                var cityName = data.cityName;
                var region = data.region;
                var detailAddress = data.detailAddress;
                var longitude = data.longitude;
                var latitude = data.latitude;
                alert("经度:" + longitude + "\n纬度:" + latitude + "\n城市名:" +
                    cityName + "\n区域:" + region + "\n详细地址:" +
                    detailAddress);
            }
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 选取图片
// 你们自己定义的方法
function onChooseImage(arg0) {
    // 调用我们的方法
    try {
        chooseImage({
            arg: arg0,
            success: function(data) {
                if(data.result == 'true') {
                    // 拿到数据之后自己处理
                    for(var i = 0; i < data.picPath.length; i++) {
                        var img = document.createElement("img");
                        img.className = "images";
                        img.src = data.picPath[i];
                        document.getElementById("cameraBody").appendChild(img);
                        var link = document.createElement("link");
                        link.setAttribute("rel", "stylesheet");
                        link.setAttribute("type", "text/css");
                        link.setAttribute("href", "../css/camera.css");
                        document.getElementById("cameraBody").appendChild(link);
                    }
                } else {
                    alert(data.message);
                }
            },
            fail: function(data) {
                alert(data);
            }
        });
    } catch(e) {
        alert(e);
    }
};
// 选取视频
function onChooseVideo() {
    chooseVideo({
        success: function(data) {
            if(data.result == 'true') {
                var videoStr = " <div><p class='btnInfo'>拍摄或从手机中选视频接口</p><button class='sbtn' onclick=onChooseVideo()>chooseVideo</button></div> <div align='center'><video src='" +
                    data.videoPath +
                    "'  width='100' height='200'" +
                    "controls='controls'>" +
                    "<source src='/i/movie.ogg' type='video/ogg'>" +
                    "<source src='/i/movie.mp4' type='video/mp4'></video></div>"
                document.getElementById("videoBody").innerHTML = videoStr;
            } else {
                alert(data.message);
            }
        },
        fail: function(data) {
            alert(data.result);
        }
    });
};

function onChooseVideoAndPic() {
    chooseVideoAndPic({
        success: function(data) {
            if(data.result == 'true') {
                var pic = data.picPath;
                var video = data.videoPath;
                if(pic != '') {
                    for(var i = 0; i < pic.length; i++) {
                        var img = document.createElement("img");
                        img.className = "images";
                        img.src = pic[i];
                        document.getElementById("cameraandvideoBody")
                            .appendChild(img);
                        var link = document.createElement("link");
                        link.setAttribute("rel", "stylesheet");
                        link.setAttribute("type", "text/css");
                        link.setAttribute("href", "../css/camera.css");
                        document.getElementById("cameraandvideoBody")
                            .appendChild(link);
                    }
                }
                if(video != '') {
                    var videoStr = " <div><p class='btnInfo'>综合媒体接口</p> <button class='sbtn' onclick=onChooseVideoAndPic()>chooseImageandVideo</button></div> <div align='center'><video src='" +
                        video +
                        "'  width='100' height='200'" +
                        "controls='controls'>" +
                        "<source src='/i/movie.ogg' type='video/ogg'>" +
                        "<source src='/i/movie.mp4' type='video/mp4'></video></div>"
                    document.getElementById("cameraandvideoBody").innerHTML = videoStr;
                }
            } else {
                alert(data.message);
            }
        },
        fail: function(data) {
            alert(data);
        }
    });
}

// 音频
var audio;
function onStartVoice() {
    startVoice({
        success: function(data) {
        },
        fail: function(data) {
            alert(data);
        }
    });
};

function onStopVoice() {
    stopVoice({
        success: function(data) {
            audio = data.audioPath;
            alert(audio);
        },
        fail: function(data) {

        }
    });

};

function onPlayVoice() {
    if(audio == null) {
        alert("请先使用startVoice接口录制一段声音");
    }
    playVoice({
        audio: audio,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};

function onStopPlayVoice() {

    stopPlayVoice({
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};

// 二维码
function onGetQRCode() {

    getQRCode({
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 分享
function onShare() {
    // 自定义分享内容
    var titleStr = "测试内容的标题"; // 标题，如果没有赋空值
    var contentStr = "这是测试的分享内容"; // 内容，如果没有赋空值
    var shareUrlStr = "http://www.baidu.com"; // 分享的链接，如果没有赋空值
    var imageStr = "http://pic.nipic.com/2007-11-09/2007119122519868_2.jpg"; // 图片的url地址,如果没有赋空值
    var dic = {
        "titleStr": titleStr,
        "contentStr": contentStr,
        "shareUrlStr": shareUrlStr,
        "imageStr": imageStr
    };
    share({
        arg: dic,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 支付
function onPay() {
    var keyy = document.getElementById("order_id").innerHTML; // 传入订单号
    var valuee = document.getElementById("good_name").innerHTML; // 传入商品名
    var keyy1 = document.getElementById("all_price").innerHTML; // 传入价格
    var valuee1 = document.getElementById("time").innerHTML; // 传入时间
    var type = '0';
    pay({
        orderNum: keyy,
        goodName: valuee,
        allPrice: keyy1,
        orderTime: valuee1,
        payType: type,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 保存数据
function onSetItem() {
    var keyy = document.getElementById("key").value; // 传入要保存数据的key
    var valuee = document.getElementById("value").value; // 传入要保存数据的value
    setItem({
        key: keyy,
        value: valuee,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 读取数据
function onGetItem() {
    var keyy = document.getElementById("keyForRead").value; // 传入需要读取数据的key值
    getItem({
        key: keyy,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 删除数据
function onRemoveItem() {
    var keyy = document.getElementById("keyForDelete").value; // 传入要移除的数据的key值
    removeItem({
        key: keyy,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 页面控制
function onShowOrHiddenNav(arg0, arg1, arg2, arg3) {
    showOrHiddenNav({
        key1: arg0,
        key2: arg1,
        key3: arg2,
        key4: arg3,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};

function oncloseWindow() {
    closeWindow({
        success: function(data) {},
        fail: function(data) {}
    });
};
// 提交
function onSubmit(arg0) {
    var content = document.getElementById("jmpinfo").value;
    var json = '{"content":' + '"' + content + '"' + '}';
    submit({
        url: arg0,
        key1: json,
        key2: videopath,
        key3: audiopath,
        key4: picPath,
        success: function(data) {
            alert(data);
            picPath = null;
            audiopath = null;
        },
        fail: function(data) {
            alert(data);
        }
    });
};

// 打电话
function onCallPhone(arg0) {
    callPhone({
        phone: arg0,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
}; // 发短信
function onSendMessage(arg0) {
    sendMessage({
        phone: arg0,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
}; // 发邮件
function onSendEmail(arg0) {
    sendEmail({
        email: arg0,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};
// 实人认证
function onFaceID(arg0) {
    faceIdentification({
        userId: arg0,
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });

};

function onAlipayID() {
    alipayIdentification({
        success: function(data) {
            alert(data);
        },
        fail: function(data) {
            alert(data);
        }
    });
};

//实名认证后查询本地用户信息
function infoText(cardid) {
    $.ajax({
        type: "post",
        url: "http://120.220.248.178:10090/SearchShlc",
        data: {
            cardid: cardid
        },
        dataType: "json",
        success: function (data) {
            lightAppJssdk.notification.hidePreloader ({
                success:function(data){ //成功回调
                },
                fail:function(data){ //错误返回
                }
            });

            if (data!=null && data.length>0){
                $("#baozhangno").text(data[0].baozhangno);
                $("#applyer").text(data[0].applyer);
                $("#cardid").text(data[0].cardid);
                $("#accepttype").text(data[0].accepttype);
                $("#taskname").text(taskname(data[0].taskname));
            }else{
                $("#baozhangno").text('无记录');
                $("#applyer").text('无记录');
                $("#cardid").text('无记录');
                $("#accepttype").text('无记录');
                $("#taskname").text(taskname('无记录'));
			}
        },
        error: function (data) {
            console.log(data);
        }
    })
}
function taskname(taskname) {
    var x=taskname;
    switch (taskname) {
        case "0" : x="备案登记";break;
        case "1": x="开始流程";break;
        case "2" : x="初审";break;
        case "3" : x="区级审核";break;
        case "4" : x="市级审核";break;
        case "5": x="市级公示";break;
        case "6" : x="市级公示中";break;
        case "7" : x="市级公示通过";break;
        case "8" : x="取得保障资格";break;
    }
    return x;
}
//获取页面传过来的信息,暂时用不到
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(decodeURI(r[2])); return null;
}